package com.bkgy.modular.tbPdiQualityRecordC642.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.tbPdiQualityRecordC642.entity.EntidElsx;
import com.bkgy.modular.tbPdiQualityRecordC642.entity.ExportLedgerPlan;
import com.bkgy.modular.tbPdiQualityRecordC642.entity.LedgerPlan;
import com.bkgy.modular.tbPdiQualityRecordC642.mapper.LedgerPlanMapper;
import com.bkgy.modular.tbPdiQualityRecordC642.service.LedgerPlanService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author
 * @version 1.0
 * @ClassName LedgerPlanController
 * @date 2023/6/13 10:11
 */
@RestController
@RequestMapping("/ledgerPlan")
public class LedgerPlanController {

    @Resource
    private LedgerPlanService ledgerPlanService;

    @Resource
    private LedgerPlanMapper ledgerPlanMapper;

    @PostMapping("/findByLedgerPlanData")
    public ResultData findByLedgerPlanData(@RequestBody JSONObject jsonObject) {
        return ledgerPlanService.findByLedgerPlanData(jsonObject);
    }

    /**
     * 列表导出功能
     * @param jsonObject
     * @param response
     */
    @PostMapping("/exportXlsx")
    public void export(@RequestBody JSONObject jsonObject, HttpServletResponse response) {
        ledgerPlanService.export(jsonObject,response);
    }

    @PostMapping("/importXlsx")
    public ResultData importXlsx(@RequestParam("file") MultipartFile multipartFile){
        if (multipartFile.isEmpty()) {
            return null;
        }
        List<LedgerPlan> list = new ArrayList<>();
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        List<String> collect1 = new ArrayList<>();
        try {
            EasyExcel.read(multipartFile.getInputStream(), EntidElsx.class, new PageReadListener<EntidElsx>(dataList -> {
                List<String> collect = dataList.stream().map(EntidElsx::getEntid).collect(Collectors.toList());
                collect1.addAll(collect);
                List<LedgerPlan> ledgerPlanList = ledgerPlanService.findByEntidData(collect);
                if(!CollectionUtils.isEmpty(ledgerPlanList)){
                    list.addAll(ledgerPlanList);
                }
            })).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        ArrayList<LedgerPlan> ledgerPlanArrayList = list.stream().collect(Collectors.collectingAndThen(
//                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(LedgerPlan::getEntid))), ArrayList::new));
        return ResultData.ResultDataSuccess(list, list.size());
    }

    @PostMapping("/exportXlsxNew")
    public void exportNew(@RequestBody JSONObject json,HttpServletResponse response){
        List<String> entids = json.getJSONArray("entids").toJavaList(String.class);
        List<LedgerPlan> ledgerPlanList = ledgerPlanService.findByEntidData(entids);
        List<ExportLedgerPlan> list = new ArrayList<>();
        for (LedgerPlan ledgerPlan : ledgerPlanList) {
            ExportLedgerPlan exportLedgerPlan = new ExportLedgerPlan();
            BeanUtils.copyProperties(ledgerPlan,exportLedgerPlan);
            list.add(exportLedgerPlan);
        }
        try {
            Map<String,Object> data = new HashMap();
            data.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\质检台账模版.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data);
            ExcelUtilsPOI.exportExcel("连退产品质量检验记录台账报表",response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
